package com.zsinfo.guoss.supplier.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.netcenter.Event.Response;
import com.example.netcenter.RetrofitBuilder;
import com.example.netcenter.Url.ConstantsCode;
import com.example.netcenter.bean.ErrorBean;
import com.example.netcenter.bean.RegisterBean;
import com.example.netcenter.bean.ResultsData;
import com.example.netcenter.bean.SystemConstantBean;
import com.google.gson.Gson;
import com.zsinfo.guoss.supplier.MainActivity;
import com.zsinfo.guoss.supplier.R;
import com.zsinfo.guoss.supplier.base.BaseActivity;
import com.zsinfo.guoss.supplier.logichelper.LoginAndRegisterHelper;
import com.zsinfo.guoss.supplier.utils.Md5util;
import com.zsinfo.guoss.supplier.utils.SharedPreferencesUtil;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by lixl on 2018/12/4.
 * <p>
 * desc: 手机号密码登录界面
 */
public class LoginPwdActivity extends BaseActivity implements View.OnClickListener {

    private EditText et_account_phone, et_account_pwd;
    private ImageView iv_account_phone_clean, iv_account_pwd_clean;
    private Button btn_login;
    private TextView btn_to_logincode;
    private TextView btn_agreement;

    @Override
    protected int getContentLayoutRes() {
        return R.layout.activity_login_pwd;
    }

    @Override
    protected void initView() {

        setTitle("登录");

        et_account_phone = findViewById(R.id.et_account_phone);
        et_account_pwd = findViewById(R.id.et_account_pwd);
        iv_account_phone_clean = findViewById(R.id.iv_account_phone_clean);
        iv_account_pwd_clean = findViewById(R.id.iv_account_pwd_clean);
        btn_login = findViewById(R.id.btn_login);
        btn_to_logincode = findViewById(R.id.btn_to_logincode);
        btn_agreement = findViewById(R.id.btn_agreement);

        et_account_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //编辑框的内容发生改变之前的回调方法
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //编辑框的内容正在发生改变时的回调方法 >>用户正在输入
                //我们可以在这里实时地 通过搜索匹配用户的输入
                if (s.toString().length() > 0) {
                    iv_account_phone_clean.setVisibility(View.VISIBLE);
                } else {
                    iv_account_phone_clean.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                //编辑框的内容改变以后,用户没有继续输入时 的回调方法
            }
        });

        et_account_pwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().length() > 0) {
                    iv_account_pwd_clean.setVisibility(View.VISIBLE);
                } else {
                    iv_account_pwd_clean.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        iv_account_phone_clean.setOnClickListener(this);
        iv_account_pwd_clean.setOnClickListener(this);
        btn_login.setOnClickListener(this);
        btn_to_logincode.setOnClickListener(this);
        btn_agreement.setOnClickListener(this);
    }

    @Override
    protected void initNetData() {

    }

    @Override
    protected void customDestroy() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_account_phone_clean:
                //清除输入的账号
                et_account_phone.setText("");
                break;
            case R.id.iv_account_pwd_clean:
                //清除输入的密码
                et_account_pwd.setText("");
                break;
            case R.id.btn_login:
                String phone = et_account_phone.getText().toString().trim();
                String pwd = et_account_pwd.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    showToast("请输入手机号");
                    return;
                }
                if (TextUtils.isEmpty(pwd)) {
                    showToast("请输入密码");
                    return;
                }

                GetNetCenter().GetNetCenter(this,
                        RetrofitBuilder.CreateApi().LOGINWihtoutTokenId(ConstantsCode.supplier_login,
                                phone,
                                Md5util.GetMD5Code(pwd)),
                        new Response() {
                            @Override
                            public void OK(Object data) {

                                getSysMes();

                                RegisterBean registerBean = (RegisterBean) data;
                                LoginAndRegisterHelper loginAndRegisterHelper = new LoginAndRegisterHelper();
                                loginAndRegisterHelper.SaveInfo(registerBean);
                                startActivity(new Intent(LoginPwdActivity.this, MainActivity.class));
                                JPushInterface.setAlias(LoginPwdActivity.this, 1, SharedPreferencesUtil.getFirmMobile()+"");  //设置别名
                                finish();
                            }

                            @Override
                            public void Fail(Throwable e) {

                                if (!e.getLocalizedMessage().contains("100000") && e.getLocalizedMessage().contains("Code")) {
                                    ErrorBean errorBean = new Gson().fromJson(e.getMessage(), ErrorBean.class);
                                    showToast(errorBean.getMsg());
                                }
                            }

                            @Override
                            public void complete() {

                            }
                        });


                break;
            case R.id.btn_to_logincode:
                startActivity(new Intent(this, LoginCodeActivity.class));
                break;
            case R.id.btn_agreement:
                startActivity(new Intent(this, AgreementActivity.class));
                break;
        }
    }

    private void getSysMes() {
        GetNetCenter().GetNetCenter(RetrofitBuilder.CreateApi().SYSTEM_CONFIG_CONSTANT(
                ConstantsCode.system_config_constant,
                SharedPreferencesUtil.getWebsiteNode()),
                new Response() {
                    @Override
                    public void OK(Object data) {
                        Log.e("lixl", data.toString());
                        ResultsData<SystemConstantBean> resultsData = (ResultsData<SystemConstantBean>) data;
                        SystemConstantBean systemConstantBean = resultsData.getData();
                        SharedPreferencesUtil.setALiPayAppId(systemConstantBean.getAlipay_app_id_new());
                        SharedPreferencesUtil.setALiPay_RSA_Private(systemConstantBean.getAlipay_rsa_private_new());
                        SharedPreferencesUtil.setALiPay_RSA_Public(systemConstantBean.getAlipay_rsa_alipay_public_new());
                    }

                    @Override
                    public void Fail(Throwable e) {
                        Log.e("lixl", e.getMessage());
                        if (!e.getLocalizedMessage().contains("100000") && e.getLocalizedMessage().contains("Code")) {
                            ErrorBean errorBean = new Gson().fromJson(e.getMessage(), ErrorBean.class);
                            showToast(errorBean.getMsg());
                        }
                    }

                    @Override
                    public void complete() {

                    }
                });
    }
}
